/**
 * Circle
 * TO FINISH
 */


function Circle(color, width)
{
	/**
	 * Initialising all the shape properties
	 */
	Shape.call(this, color, width);
	
	/**
	*
	*/
	this.shapeId = 1;
	
	/**
	 * The circle radius
	 */
	this.radius = 24;
	
	/**
	 * Number of segments (Increase this value for better quality)
	 */
	this.nb_segs = 64;
	
	/**
	 * How fast is the circle redrawn (ms)
	 */
	this.speed = 10;
	
	/**
	 * 
	 */
	this.center_x;
	
	
	/**
	 * 
	 */
	this.center_y;
	
	/**
	 * 
	 */
	this.id = 1;
	
	this.computeCircle = function()
	{
		var x_start = this.center_x;
		var x_end = this.center_x;
		
		var y_start = this.center_y;
		var y_end = this.center_y;
		
		for (var i = 0 ; i < this.nb_segs ; i++)
		{
			x_start = x_start + (Math.sin((i) / this.nb_segs * 2 * Math.PI) * this.radius);
			y_start = y_start + (Math.cos((i) / this.nb_segs * 2 * Math.PI) * this.radius);
			
			x_end = x_end + (Math.sin((i+1) / this.nb_segs * 2 * Math.PI) * this.radius);
			y_end = y_end + (Math.cos((i+1) / this.nb_segs * 2 * Math.PI) * this.radius);
			
			this.addPoint(x_start, y_start, this.speed);
			this.addPoint(x_end, y_end, this.speed);
			
			x_start = this.center_x;	y_start = this.center_y;
			x_end = this.center_x;		y_end = this.center_y;
			
		}
	};
	
	this.initShape = function(x,y)
    {
		
		this.center_x = x;
		this.center_y = y;
		
		this.computeCircle();
    };
    
    this.fillShape = function(x,y)
    {
    	 this.radius = Math.sqrt((y - this.center_y) * (y - this.center_y) +
    			 				 (x - this.center_x) * (x - this.center_x));
    	 
    	 this.flushBuffers();
    	 
    	 this.computeCircle();
    };
    
    this.endShape = function()
    {
    	this.addToCache(true);
    };
    
    this.isMouseHover = function(x,y)
    {
    	var distance = (x - this.center_x)*(x - this.center_x) + (y - this.center_y)*(y - this.center_y);
    	
    	if (distance > (this.radius * this.radius))
    		return false;
    	
    	return true;
    };
	
	this.isMouseOverPath = function(x, y){
		var distance = (x - this.center_x)*(x - this.center_x) + (y - this.center_y)*(y - this.center_y);
    	
    	if (distance > (this.radius * this.radius))
    		return false;
    	
    	return true;
	}
    
    this.move = function(nbx,nby)
    {
    	
    	this.center_x = this.center_x - nbx;
    	this.center_y = this.center_y - nby;
    	
    	this.flushBuffers();
    	
    	this.computeCircle();
    	
    	this.real_x = this.real_x - nbx;
    	this.real_y = this.real_y - nby;
    	
		
		this.computeExtremePoints();
    	
    	
    };
    
    this.getBinarySize = function()
    {
    	var ret = 1; // 1 because of shape id
		
		
		//ret += this.x.length * 10; // x, y, time
		
		ret += 1; // Thickness
		
		ret += 4; // Color
		
		ret += 4; // real_x / real_y
		
		// Transformations
		ret += 8;
		
		// BreakPoints
		
		ret += 2;
		
		
		for (var i = 0 ; i < this.breakPoints.length ; i++){
			ret += 2
		}
		
		// Radius
		ret += 1;
		
		// Optionel
		ret += 2;
		
		return ret;
    };
	
	this.generateHexString = function(data, byteoffset)
	{
		// Setting general infos
		//data.setUint16(byteoffset, this.x.length); byteoffset += 2; // x and y length
		
		// Setting transformations
		data.setFloat32(byteoffset, this.scale_x); byteoffset += 4;
		data.setFloat32(byteoffset, this.scale_y); byteoffset += 4;
		
		// Setting the number of breakpoints
		data.setUint16(byteoffset, this.breakPoints.length);  byteoffset += 2;
		
		// Setting the breakpoints
		for (var i = 0 ; i < this.breakPoints.length ; i++){
			data.setUint16(byteoffset, this.breakPoints[i]);  byteoffset += 2;
		}
		
		// Setting coords
		data.setUint16(byteoffset, this.real_x); byteoffset +=2;
		data.setUint16(byteoffset, this.real_y); byteoffset +=2;
		
		// Setting radius
		
		
		// Setting styles
		data.setUint8(byteoffset, this.thickness);	byteoffset++;
		
		data.setUint8(byteoffset, this.color.red);	byteoffset++;
		
		data.setUint8(byteoffset, this.color.green); byteoffset++;
		
		data.setUint8(byteoffset, this.color.blue);	byteoffset++;
		
		data.setUint8(byteoffset, this.color.alpha); byteoffset++;
		
		// Setting the data
		
		for (var i = 0 ; i < this.x.length ; i++)
		{
			data.setFloat32(byteoffset, this.x[i]); byteoffset+=4;
			data.setFloat32(byteoffset, this.y[i]); byteoffset+=4;
			
			// Optimise room here !
			data.setUint16(byteoffset, this.time[i]); byteoffset+=2;
		}
		
		// We need this return in order to continue the binary reading at the good offset !
		return byteoffset;
	};
	
	this.loadFromFile = function(data, byteoffset)
	{
		// Purging the shape
		this.flushBuffers();
		
		// Getting general infos
		var length = data.getUint16(byteoffset); byteoffset += 2; // Length for x,y,time
		
		console.log("taille " + length);
		
		// Setting transformations
		this.scale_x = data.getFloat32(byteoffset); byteoffset += 4;
		this.scale_y = data.getFloat32(byteoffset); byteoffset += 4;
		
		// Getting the number of breakpoints
		var numberOfBreakPoints = data.getUint16(byteoffset);  byteoffset += 2;
		
		// Getting the breakpoints
		for (var i = 0 ; i < numberOfBreakPoints ; i++){

			this.breakPoints.push(data.getUint16(byteoffset)); byteoffset += 2;
		}
		
		
		// Getting coords
		this.real_x = data.getUint16(byteoffset); byteoffset += 2;
		this.real_y = data.getUint16(byteoffset); byteoffset += 2;
		
		// Getting styles
		this.thickness = data.getUint8(byteoffset); byteoffset++;
		
		
		
		this.color.red = data.getUint8(byteoffset); byteoffset++;
		
		this.color.green = data.getUint8(byteoffset); byteoffset++;
		
		this.color.blue = data.getUint8(byteoffset); byteoffset++;
		
		this.color.alpha = data.getUint8(byteoffset); byteoffset++;
		
		//console.log(this.color.toString());

		
		// Getting the data
		for (var i = 0 ; i < length ; i++)
		{
			this.addPoint(data.getUint16(byteoffset), 
						  data.getUint16(byteoffset+2),
						  data.getUint16(byteoffset+4));
						  
			byteoffset += 6;
		}
		
		// Add in cache once it's finished
		this.addToCache(false);
		
		return byteoffset;
	};
}

Shape.prototype = new Circle();